package org.amq.server.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jms.TextMessage;
import javax.servlet.http.HttpServletRequest;

import org.amq.server.domain.MessageView;
import org.apache.activemq.broker.jmx.BrokerViewMBean;
import org.apache.activemq.broker.jmx.QueueViewMBean;
import org.apache.activemq.broker.jmx.SubscriptionViewMBean;
import org.apache.activemq.command.Message;
import org.apache.activemq.web.LocalBrokerFacade;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping(value = "/")
public class AdminController {
    @Autowired
    LocalBrokerFacade localBrokerFacade;

    private void setCommon(Model model) throws Exception {
        BrokerViewMBean brokerViewMBean = localBrokerFacade.getBrokerAdmin();
        model.addAttribute("broker", brokerViewMBean);
        model.addAttribute("cursite", System.getProperty("mq.cur.site"));
        // model.addAttribute("cururl", get().get(System.getProperty("mq.cur.site").toLowerCase()));
        model.addAttribute("map", get());
    }

    @RequestMapping("/admin/index.html")
    public String getOtherQueues(Model model, RedirectAttributes redirectAttributes, HttpServletRequest request)
            throws Exception {
        setCommon(model);
        return "index/home";
    }

    @RequestMapping("/admin/queues.html")
    public String queues(Model model, String like, RedirectAttributes redirectAttributes, HttpServletRequest request)
            throws Exception {
        setCommon(model);
        like = like.toUpperCase();
        model.addAttribute("like", like.toUpperCase());
        Comparator<QueueViewMBean> compare = new Comparator<QueueViewMBean>() {
            @Override
            public int compare(QueueViewMBean o1, QueueViewMBean o2) {
                // TODO Auto-generated method stub
                return o1.getName().compareToIgnoreCase(o2.getName());
            }
        };
        Collection<QueueViewMBean> list = localBrokerFacade.getQueues();
        model.addAttribute("queues", list);

        return "index/queues";
    }

    @RequestMapping("/admin/queue")
    public String queue(Model model, String like, RedirectAttributes redirectAttributes, HttpServletRequest request)
            throws Exception {
        setCommon(model);
        like = like.toUpperCase();
        model.addAttribute("like", like.toUpperCase());
        Collection<QueueViewMBean> list = localBrokerFacade.getQueues();
        if (StringUtils.isBlank(like)) {
            model.addAttribute("queues", list);
        }
        else {
            List<QueueViewMBean> list2 = new ArrayList<QueueViewMBean>();
            for (QueueViewMBean b : list) {
                for (String key : like.split(",")) {
                    if (b.getName().equalsIgnoreCase(key)) {
                        list2.add(b);
                    }
                }
            }
            model.addAttribute("queues", list2);
        }
        model.addAttribute("pageType", "none");
        return "index/queue";
    }

    @RequestMapping("/admin/messages")
    public String messages(Model model, String queue, RedirectAttributes redirectAttributes, HttpServletRequest request)
            throws Exception {
        setCommon(model);
        QueueViewMBean queview = localBrokerFacade.getQueue(queue);
        List<MessageView> mviews = new ArrayList<MessageView>();
        try {
            if (queview != null) {
                List<?> msgs = queview.browseMessages();
                if (CollectionUtils.isNotEmpty(msgs)) {
                    for (int i = 0; i < msgs.size(); i++) {
                        if (msgs.get(i) instanceof Message) {
                            MessageView mv = new MessageView();
                            mv.setMsg((Message) msgs.get(i));
                            if (mv.getMsg().getMessage() instanceof TextMessage) {
                                mv.setContent(((TextMessage) mv.getMsg().getMessage()).getText());
                            }
                            mviews.add(mv);
                        }
                    }
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("msgs", mviews);
        return "index/messages";
    }

    @RequestMapping("/admin/show.html")
    public String SITE(Model model, String from, String to, RedirectAttributes redirectAttributes,
            HttpServletRequest request) throws Exception {
        setCommon(model);
        model.addAttribute("from", from);
        model.addAttribute("to", to);
        return "index/queueall";
    }

    @RequestMapping("/admin/topics.html")
    public String topics(Model model, RedirectAttributes redirectAttributes, HttpServletRequest request)
            throws Exception {
        setCommon(model);
        model.addAttribute("topics", localBrokerFacade.getTopics());
        return "index/topics";
    }

    @RequestMapping("/admin/connections.html")
    public String connections(Model model, RedirectAttributes redirectAttributes, HttpServletRequest request)
            throws Exception {
        setCommon(model);
        model.addAttribute("connectors", localBrokerFacade.getConnectors());
        model.addAttribute("nets", localBrokerFacade.getNetworkConnectors());
        model.addAttribute("connections", localBrokerFacade.getConnections());
        return "index/connections";
    }

    @RequestMapping("/admin/delete.html")
    public String delete(Model model, String queueName, RedirectAttributes redirectAttributes,
            HttpServletRequest request) throws Exception {
        localBrokerFacade.getBrokerAdmin().removeQueue(queueName);
        return "redirect:/admin/queues.html?like=";
    }

    @RequestMapping("/admin/consumers.html")
    public String consumers(Model model, String queueName, RedirectAttributes redirectAttributes,
            HttpServletRequest request) throws Exception {
        Collection<SubscriptionViewMBean> consm = localBrokerFacade.getQueueConsumers(queueName);
        model.addAttribute("consumers", consm);
        model.addAttribute("pageType", "none");
        return "index/consumers";
    }

    private Map get() {
        Map map = new HashMap();
        map.put("local", "http://192.168.16.180:61613");
        map.put("tc", "http://192.168.16.180:61613");
        map.put("usa", "http://192.168.16.180:61613");
        map.put("cnc", "http://192.168.16.180:61613");
        return map;
    }

}
